// Author: Simon Oberhammer
// License: MIT (http://www.opensource.org/licenses/mit-license.php)

/**
 * load content and comments
 * update document.title and #anchor  = title_UID
 */
mailog.loadPost = function(postKey) {
    var postKey = postKey;
    var postUrl = mailog.baseUrl + "posts/" + postKey + ".html";
    var commentUrl = mailog.baseUrl + "comments/" + postKey + ".html";
    $("#post").load(postUrl);
    $("#comments").load(commentUrl, {}, function (responseText, textStatus, XMLHttpRequest) {
                                        if (textStatus == "error") {
                                            $("#comments").html("");
                                        }
                                    });
    var postTitle = postKey;
    var postParts = [];
    if (postKey.split) {
        postParts = postKey.split("$$");
        if (postParts.length && postParts[0]){
            postTitle = postParts[0];
        }
    }
    window.location.hash = "#" + postTitle;
    this.updateMenuHighlight(postKey);
}


/**
 * highlight the selected post in the postlist
 */
mailog.updateMenuHighlight = function(postKey) {
    $("a").removeClass("postlist-selected");
    $("a[href='#" + postKey + "']").addClass("postlist-selected");
};

/**
 * init, load post list, firstpost and set title, description etc.
 * load the #anchor-key post
 */
$(document).ready(function() {
    var menuUrl = mailog.baseUrl + "postlist.html";
    $("#mailog-title-link").html(mailog.title);
    document.title = mailog.title;
    $("meta[name='keywords']").attr("content", mailog.meta_keywords)
    $("meta[name='description']").attr("content", mailog.meta_description)
    $("#mailog-description").html(mailog.description);
    // schaun ob ein #anchor gesetzt ist. wenn schon dann mailog.loadPost machen
    var postKey = ["firstpost-0"]
    if (window.location.hash) {
        postKey = window.location.hash.substr(1);
    } 
    mailog.loadPost(postKey);
    $("#postlist").load(menuUrl, {}, function() {
                                mailog.updateMenuHighlight(postKey);
                            });


});

